﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace C4DevContest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            myGame = new Connect4Board();
            myAI = new RandomPlayer();
        }


        private Connect4Board myGame;
        private IConnect4AI myAI;


        private void Form1_Load(object sender, EventArgs e)
        {
            Random myRan = new Random();

            if (myRan.Next() % 2 == 1)
            {
                MessageBox.Show("Human goes first.");
                myAI.SetMyGamePiece(GamePiece.Red);
            }
            else
            {
                MessageBox.Show("AI goes first.");
                myAI.SetMyGamePiece(GamePiece.Black);
                GetAIMove();
            }    
        }


        private void GetAIMove()
        {
            if (!myGame.IsGameOver())
            {
                int AIMove = myAI.GetMove(myGame);
                myGame.Move(AIMove);

                Invalidate();
            }
        }


        protected override void OnPaint(PaintEventArgs e)
        {
            Graphics g = e.Graphics;

            System.Drawing.SolidBrush redBrush = new SolidBrush(Color.Red);
            System.Drawing.SolidBrush blackBrush = new SolidBrush(Color.Black);

            g.FillRectangle(new SolidBrush(Color.White), 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height);


            g.ScaleTransform(1.0F, -1.0F);
            g.TranslateTransform(0, -this.ClientRectangle.Height);

            int BoxWidth = this.ClientRectangle.Width / 7;
            int BoxHeight = this.ClientRectangle.Height / 6;

            for (int i = 0; i < 7; i++)
            {
                for (int j = 0; j < 6; j++)
                {
                    GamePiece myPiece = myGame.GetPiece(i, j);

                    if (myPiece == GamePiece.Black)
                        g.FillRectangle(blackBrush, BoxWidth * i, BoxHeight * j, BoxWidth, BoxHeight);
                    if (myPiece == GamePiece.Red)
                        g.FillRectangle(redBrush, BoxWidth * i, BoxHeight * j, BoxWidth, BoxHeight);

                    g.DrawRectangle(Pens.Gray, BoxWidth * i, BoxHeight * j, BoxWidth, BoxHeight);
                }
            }

            if (myGame.IsGameOver())
            {
                var thing = myGame.WeHaveAWinner();

                if (thing == myAI.GetMyGamePiece())
                    MessageBox.Show(string.Format("{0} wins!", myAI.GetAIName()));
                else if (thing == GamePiece.None)
                    MessageBox.Show("Game over.  It's a tie.");
                else
                    MessageBox.Show("Stupid human wins.  Robot apocalypse postponed until bugs are fixed.");
            }
        }


        private void Form1_MouseClick(object sender, MouseEventArgs e)
        {
            if (!myGame.IsGameOver())
            {
                int BoxWidth = this.ClientRectangle.Width / 7;
                int MoveBox = e.Location.X / BoxWidth;

                myGame.Move(MoveBox);
                Invalidate();

                GetAIMove();
            }
        }
    }
}
